Passed
Pull Request — master (#20)
by Muhammad Dyas
01:34
created

BaseHandler   A

Complexity

Total Complexity 12

Size/Duplication

Total Lines 28
Duplicated Lines 0 %

Importance

Changes 0
Metric Value
wmc 12
eloc 23
dl 0
loc 28
rs 10
c 0
b 0
f 0

4 Functions

Rating   Name   Duplication   Size   Complexity  
A getAnnotations 0 3 4
A process 0 2 1
B getUserTimezone 0 10 6
A getAnnotationByType 0 3 1
1
import {chat_v1 as chatV1} from 'googleapis/build/src/apis/chat/v1';
2
import {LocaleTimezone} from '../helpers/interfaces';
3
4
export default abstract class BaseHandler {
5
  protected event: chatV1.Schema$DeprecatedEvent;
6
7
  constructor(event: chatV1.Schema$DeprecatedEvent) {
8
    this.event = event;
9
  }
10
11
  protected getAnnotations(): chatV1.Schema$Annotation[] {
12
    return this.event.message?.annotations ?? [];
13
  }
14
15
  protected getAnnotationByType(type: string): chatV1.Schema$Annotation | undefined {
16
    return this.getAnnotations().find((annotation) => annotation.type === type);
17
  }
18
19
  protected getUserTimezone(): LocaleTimezone {
20
    if (this.event.common?.timeZone?.id) {
21
      const locale = this.event.common.userLocale;
22
      const id = this.event.common.timeZone.id;
23
      const offset = this.event.common.timeZone.offset ?? 0;
24
      return {locale, id, offset};
25
    }
26
27
    return {'locale': 'en', 'offset': 0, 'id': 'UTC'};
28
  }
29
30
  public abstract process(): chatV1.Schema$Message | Promise<chatV1.Schema$Message>;
31
}
32